package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.DiagnosisAndTreatmentItem

/**
 * 诊疗服务中条目所用适配器,此适配器在医院综合页面底部条目、个人中心条目中被复用
 */
class ServiceFragmentAdapter(context: Context) :
    RecyclerView.Adapter<ServiceFragmentAdapter.ServiceHolder>() {
    private val context = context
    lateinit var dataList: MutableList<DiagnosisAndTreatmentItem>
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class ServiceHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val itemIcon: ImageView = itemView.findViewById(R.id.inpatient_and_outpatient_item_icon)
        val itemName: TextView = itemView.findViewById(R.id.inpatient_and_outpatient_item_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServiceHolder {
        val serviceHolder = ServiceHolder(
            LayoutInflater.from(context)
                .inflate(R.layout.inpatient_and_outpatient_item, parent, false)
        )
        serviceHolder.itemView.setOnClickListener {
            listener(serviceHolder.adapterPosition)
        }
        return serviceHolder
    }

    override fun onBindViewHolder(holder: ServiceHolder, position: Int) {
        holder.itemIcon.setImageResource(dataList[position].itemIcon)
        holder.itemName.text = dataList[position].itemName
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}